package simulaco.business;

import gminet.infra.business.BusinessException;
import gminet.infra.business.BusinessObject;
import gminet.infra.db.Transaction;
import gminet.infra.db.TransactionException;

import java.sql.Connection;
import java.util.List;

import simulaco.dao.QuestaoComplementoDAO;
import simulaco.dao.QuestaoDAO;
import simulaco.dto.ConcursoMateria;
import simulaco.dto.QuestaoComplemento;

public class QuestaoComplementoBusiness extends BusinessObject {

    public static List<QuestaoComplemento> findByConcursoMateria(ConcursoMateria concursoMateria, Connection conn)
            throws TransactionException {

        return QuestaoComplementoDAO.findByConcursoMateria(concursoMateria, conn);
    }

    public static QuestaoComplemento insert(QuestaoComplemento questaoComplemento) throws BusinessException,
            TransactionException {

        QuestaoComplemento newQuestaoComplemento = null;
        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            // QUESTAO COMPLEMENTO
            insert(newQuestaoComplemento, conn);
            // COMMIT
            transaction.commit();
            //
            newQuestaoComplemento = questaoComplemento;
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
        return newQuestaoComplemento;
    }

    protected static QuestaoComplemento insert(QuestaoComplemento questaoComplemento, Connection conn)
            throws BusinessException, TransactionException {

        // QUESTAO COMPLEMENTO
        ConcursoMateria concursoMateria = questaoComplemento.getConcursoMateria();
        questaoComplemento.setCodigo(QuestaoComplementoDAO.getNextCodigo(concursoMateria, conn));
        QuestaoComplementoDAO.insert(questaoComplemento, conn);
        //
        return questaoComplemento;
    }

    public static void update(QuestaoComplemento questaoComplemento) throws BusinessException, TransactionException {

        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            update(questaoComplemento, conn);
            // COMMIT
            transaction.commit();
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
    }

    protected static void update(QuestaoComplemento questaoComplemento, Connection conn) throws BusinessException,
            TransactionException {

        QuestaoComplementoDAO.update(questaoComplemento, conn);
    }

    public static void delete(QuestaoComplemento questaoComplemento) throws BusinessException, TransactionException {

        Transaction transaction = createTransaction();
        try {
            Connection conn = transaction.getConnection();
            delete(questaoComplemento, conn);
            transaction.commit();
        } catch (TransactionException te) {
            transaction.rollback();
            throw te;
        } finally {
            transaction.release();
        }
    }

    protected static void delete(QuestaoComplemento questaoComplemento, Connection conn) throws BusinessException,
            TransactionException {

        if (QuestaoDAO.findByComplemento(questaoComplemento.getCodigo(), conn).size() == 0) {
            QuestaoComplementoDAO.delete(questaoComplemento, conn);
        }
    }
    
}
